package com.controller;


import com.pojo.User;
import com.service.FindUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/user")
@Controller
public class logincontroller {
    @Autowired
    private FindUserService finduservice;
    @RequestMapping("/login_show")
    public String login_show(){
        return "adminlogin";

    }

    /*
       接收前端表格传来user数据
       并执行查询校验
     */

    @RequestMapping("/login")
    public String loginpage(@RequestParam(value = "uname", required = false)String uname, @RequestParam(value = "upassword", required = false)String upassword , HttpSession session ,Model model){
        User user =new User();
        user.setUname(uname);
        user.setUpassword(upassword);
        System.out.println(user.toString());
        if(uname!=null&&upassword!=null){
            User user1=finduservice.adminLogin(user);
            HashMap<String,Integer> map= new HashMap<String,Integer>();
            map.put("StartPage",1);
            map.put("PageSize",2);
            List<User> list =finduservice.getUserByPage(map);
            for (User user2:list
                    ) {
                System.out.println(user2);

            }
            if(user1==null) {
                model.addAttribute("error","用户名或密码不正确");
                return "adminlogin";
            }
            //将user内容存在会话中
            session.setAttribute("AdminUser",user1);
            return "loginmanager";
        }
        return "adminlogin";
    }
    @RequestMapping("/index")
    public String welcome(){
        return "index";

    }

    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "adminlogin";
    }
     @RequestMapping("/getUserByPage")
         public void getuserByLimit (){
            HashMap<String,Integer> map= new HashMap<String,Integer>();
            map.put("StartPage",1);
         map.put("PageSize",1);
          List<User> list =finduservice.getUserByPage(map);
         for (User user:list
              ) {
             System.out.println(user);

         }

        }
        @RequestMapping("/getList")
        @ResponseBody
       public List<User> getuserList(){
           List<User> list=finduservice.getuserList();
           return list;
       }

}
